﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using TouchRC.ListBoxes;
using TouchRC.Classes;
using TouchRC.Logic;
using TouchRC.Themes;
using Fluid.Drawing.GdiPlus;
using System.Collections.Specialized;

namespace TouchRC.Templates
{
    public class ComputerTemplate : TransparentTemplate
    {
        private FluidLabel m_ComputerLabel;
        private FluidButton m_EditButton;
        private FluidButton m_DeleteButton;
        private bool m_EditMode;
        private int m_Border = (AppSettings.ButtonDimensions / 8);

        protected override void Initialize()
        {
        }

        protected override void InitControl()
        {
            base.InitControl();

            int listboxHeight = AppSettings.ButtonDimensions + (2 * m_Border);
            int listboxWidth = AppSettings.ScreenResolution.Width - (int)(AppSettings.ScreenResolution.Width * 0.2);

            this.Bounds = new Rectangle(0, 0, listboxWidth, listboxHeight);

            int labelXCoords = m_Border * 3;
            int deleteButtonXCoords = listboxWidth - (m_Border + AppSettings.ButtonDimensions);
            int editButtonXCoords = deleteButtonXCoords - (m_Border + AppSettings.ButtonDimensions);

            m_ComputerLabel = new FluidLabel(string.Empty, labelXCoords, m_Border,
                (listboxWidth - (2*labelXCoords)), AppSettings.ButtonDimensions);
            m_ComputerLabel.LineAlignment = StringAlignment.Center;
            m_ComputerLabel.Font = Theme.CurrentTheme.StandardFont;

            m_EditButton = new FluidButton(string.Empty, editButtonXCoords, m_Border, 
                AppSettings.ButtonDimensions, AppSettings.ButtonDimensions);
            m_EditButton.Shape = ButtonShape.Flat;
            m_EditButton.Image = ImageFactory.GetBitmap(ImageTypes.Cogs);
            m_EditButton.Click += new EventHandler(editButton_Click);

            m_DeleteButton = new FluidButton(string.Empty, deleteButtonXCoords, m_Border, 
                AppSettings.ButtonDimensions, AppSettings.ButtonDimensions);
            m_DeleteButton.Shape = ButtonShape.Flat;
            m_DeleteButton.Image = ImageFactory.GetBitmap(ImageTypes.Cross);
            m_DeleteButton.Click += new EventHandler(deleteButton_Click);

            DrawTemplate();

            Controls.Add(m_EditButton);
            Controls.Add(m_DeleteButton);
            Controls.Add(m_ComputerLabel);

            m_EditButton.Anchor = AnchorTR;
            m_ComputerLabel.Anchor = AnchorAll;
        }

        public override void OnGesture(GestureEventArgs e)
        {
            if (e.Gesture == Gesture.PressAndHold)
            {
                EditMode = !EditMode;
            }
            else
            {
                base.OnGesture(e);
            }
        }

        public bool EditMode
        {
            get { return m_EditMode; }
            set
            {
                if (m_EditMode != value)
                {
                    m_EditMode = value;
                    OnEditModeChanged();
                }
            }
        }

        private void OnEditModeChanged()
        {
            DrawTemplate();
        }

        private void DrawTemplate()
        {
            BeginInit();

            m_EditButton.Visible = m_EditMode;
            m_DeleteButton.Visible = m_EditMode;

            if (m_EditMode)
            {
                int editButtonXCoords = m_EditButton.Bounds.X;
                int width = this.Bounds.Width - m_Border - editButtonXCoords;
                m_ComputerLabel.Width = width;
                m_ComputerLabel.Alignment = StringAlignment.Near;
            }
            else
            {
                int labelXCoords = m_ComputerLabel.Bounds.X;
                int width = this.Bounds.Width - (2 * labelXCoords);
                m_ComputerLabel.Width = width;
                m_ComputerLabel.Alignment = StringAlignment.Center;
            }

            EndInit();
        }

        protected override void OnItemUpdate(object value)
        {
            base.OnItemUpdate(value);
            ComputerListBoxItem item = value as ComputerListBoxItem;
            if (item != null)
            {
                m_ComputerLabel.Text = item.Name;
            }
            else
            {
                m_ComputerLabel.Text = string.Empty;
            }
        }

        void editButton_Click(object sender, EventArgs e)
        {
            ComputerListBoxItem item = Item as ComputerListBoxItem;
            if (item != null)
            {
                ComputerConfigPanel ccp = new ComputerConfigPanel(ComputerConfigState.Edit);
                ccp.Computer = item;
                ccp.ShowMaximized(ShowTransition.FromBottom);
            }
        }

        void deleteButton_Click(object sender, EventArgs e)
        {
            ComputerListBoxItem item = Item as ComputerListBoxItem;
            if (item != null)
            {
                MessageDialog dialog = new MessageDialog();
                dialog.Message = String.Format("Delete '{0}'?", item.Name);
                dialog.Result += new EventHandler<Fluid.Classes.DialogEventArgs>(dialog_Result);
                dialog.ShowModal(ShowTransition.FromBottom);
            }
        }

        void dialog_Result(object sender, Fluid.Classes.DialogEventArgs e)
        {
            switch (e.Result)
            {
                case System.Windows.Forms.DialogResult.OK:
                    ComputerListBoxItem item = Item as ComputerListBoxItem;
                    if (item != null)
                    {
                        ConfigSettings.RemoveComputerConfigs(item);
                    }
                    //ComputerSelectionPanel.Instance.RefreshValues();
                    break;
            }
        }
    }
}
